package edu.udlap.spring2008.is300.finalexam.app.mvc.view;

import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.ModelState;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public class ViewImageSlideShowControls extends ViewComponent implements
        ActionListener
{

    protected JPanel mainPanel_ = new JPanel();
    protected JPanel controlsPanel_ = new JPanel();
    protected JPanel imagePanel_ = new JPanel();
    
    protected JLabel imageLabel_ = new JLabel();
    protected JButton firstButton_ = new JButton("<html>" +
            "<img src=\"file:imgs/first.png\">" +
            "</html>");
    protected JButton previousButton_ = new JButton("<html>" +
            "<img src=\"file:imgs/previous.png\">" +
            "</html>");
    protected JButton nextButton_ = new JButton("<html>" +
            "<img src=\"file:imgs/next.png\">" +
            "</html>");
    protected JButton lastButton_ = new JButton("<html>" +
            "<img src=\"file:imgs/last.png\">" +
            "</html>");

    public ViewImageSlideShowControls()
    {
        firstButton_.addActionListener(this);
        previousButton_.addActionListener(this);
        nextButton_.addActionListener(this);
        lastButton_.addActionListener(this);
        
        controlsPanel_.add(firstButton_);
        controlsPanel_.add(previousButton_);
        controlsPanel_.add(nextButton_);
        controlsPanel_.add(lastButton_);
        
        imagePanel_.add(imageLabel_);
        
        BorderLayout layout = new BorderLayout(10,10);
        
        mainPanel_.setLayout(layout);
        
        mainPanel_.add(imagePanel_, BorderLayout.CENTER);
        mainPanel_.add(controlsPanel_, BorderLayout.NORTH);
    }
    
    @Override
    public void setInitialState(ModelState modelState)
    {
        selectionChanged(modelState);
    }

    @Override
    public void imageSetChanged(ModelState modelState)
    {
        ///do nothing...
    }

    @Override
    public void selectionChanged(ModelState modelState)
    {
        ImageRecord imageRecord =
                modelState.getSelectedImageRecord();
        
        imageLabel_.setText("No image selected. Use navigation controls to " +
                "start.");
        
        if(imageRecord == null)
            return;
        
        imageLabel_.setText("<html>" +
                            "   <img src=\"file:" + imageRecord.getPath() + 
                            "\">" +
                            "</html>");
    }

    @Override
    public void favoritesChanged(ModelState modelState)
    {
        ///do nothing...
    }

    @Override
    public void slideShowMode()
    {
        ///do nothing...
    }

    @Override
    public void gridMode()
    {
        ///do nothing...
    }

    @Override
    public Component getComponent()
    {
        return (Component)mainPanel_;
    }

    public void actionPerformed(ActionEvent e)
    {
        if(controller_ == null)
            return;
        
        Object source = e.getSource();
        
        if(source == firstButton_)
        {
            controller_.firstImageRecord();
        }
        else if(source == previousButton_)
        {
            controller_.previousImageRecord();
        }
        else if(source == nextButton_)
        {
            controller_.nextImageRecord();
        }
        else if(source == lastButton_)
        {
            controller_.lastImageRecord();
        }
    }

}
